package com.caiwm.client.view;

import com.caiwm.client.common.MessageEntity;
import com.caiwm.client.common.MessageType;
import com.caiwm.client.dialog.CaiwmDialog;
import com.caiwm.client.protocol.TCPUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃  神兽保佑
 * 　　　　┃　　　┃  代码无bug
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 * </pre>
 *
 * @author shiqla
 * @Desc 聊天主面板
 * @Date 2019/1/11/011
 */
public class ChatView extends CaiwmDialog implements ActionListener {

    private JFrame jFrame;

    private List<JComponent> list;

    private List<PrivateChatView> privateChatViewList;

    private JPanel leftPanel;

    private JPanel rightPanel;

    private JPanel rightTopPanel;

    private JPanel rightUnderPanel;

    private JTextArea messageTextArea;

    private JTextArea inputMessageTextArea;

    private JButton sendMessageButton;

    private String username;

    private boolean isOpenPrivateWindow = false;

    public ChatView(String username) {
        this.username = username;
        list = new ArrayList<JComponent>();
        privateChatViewList = new ArrayList<PrivateChatView>();
        jFrame = new JFrame("亲爱的 " + this.username + " 欢迎来到网络聊天室..");
        jFrame.setLayout(null);
        jFrame.setSize(880, 500); //设置窗口的大小
        jFrame.setLocation(300, 200);//设置窗口的初始位置
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
    }

    public void init() {
        leftPanel = new JPanel();
        leftPanel.setSize(130, 500);
        leftPanel.setBackground(new Color(224, 224, 224));

        rightPanel = new JPanel();
        rightPanel.setBackground(new Color(230, 85, 118));
        rightPanel.setBounds(130, 0, 750, 500);
        rightPanel.setLayout(null);

        rightTopPanel = new JPanel();
        rightTopPanel.setBackground(new Color(226, 136, 49));

        rightUnderPanel = new JPanel();
        rightUnderPanel.setBackground(new Color(26, 172, 229));

        this.messageTextArea = new JTextArea();
        this.messageTextArea.setBounds(0, 0, 740, 300);
        this.messageTextArea.setLineWrap(true);
        this.messageTextArea.setBackground(new Color(38, 255, 161));
        this.messageTextArea.setEditable(false);
        this.messageTextArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));


        this.inputMessageTextArea = new JTextArea();
        this.inputMessageTextArea.setBounds(0, 310, 740, 120);
        this.inputMessageTextArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));

        this.sendMessageButton = new JButton("发送");
        this.sendMessageButton.setBounds(605, 430, 130, 30);
    }

    public void addComponent() {
        JScrollPane jScrollPane = new JScrollPane(this.leftPanel);
        jScrollPane.setSize(130, 480);

        JScrollPane jScrollPane1 = new JScrollPane(this.messageTextArea);
        jScrollPane1.setBounds(0, 0, 740, 300);

        JScrollPane jScrollPane2 = new JScrollPane(this.inputMessageTextArea);
        jScrollPane2.setBounds(0, 310, 740, 120);

        this.rightPanel.add(jScrollPane1);
        this.rightPanel.add(jScrollPane2);
        this.rightPanel.add(sendMessageButton);

        this.jFrame.add(jScrollPane);
        this.jFrame.add(rightPanel);

    }

    private void bindListener() {
        this.sendMessageButton.addActionListener(this);
    }

    public void addUser() {
        this.leftPanel.setLayout(new GridLayout(50, 1));
        JLabel jLabel = new JLabel("当前在线用户");
        jLabel.setSize(130, 30);
        this.leftPanel.add(jLabel);
    }


    public void start() {
        MessageEntity messageEntity = new MessageEntity(this.username,MessageType.online,"");
        TCPUtils.getInstance().sendMessage(messageEntity);
        this.init();
        this.addComponent();
        bindListener();
        this.addUser();
        Thread thread = new Thread(new ReivceThread(this));
        thread.start();

        this.jFrame.setResizable(false);
        this.jFrame.setVisible(true);
    }

    public void destory() {
        this.jFrame.dispose();
    }


    //监听发送按钮，动作
    public void actionPerformed(ActionEvent e) {

        StringBuilder stringBuilder = new StringBuilder(this.messageTextArea.getText());
        stringBuilder.append("你的消息:" + "\n");

        String message = this.inputMessageTextArea.getText();
        if(message == null || message.equals("")){
            this.errorMessageDialog("无法发送空的消息！");
        }else{
            message = "        " + message + "\n";
            stringBuilder.append(message);
            MessageEntity messageEntity = new MessageEntity(this.username,MessageType.message,message);
            TCPUtils.getInstance().sendMessage(messageEntity);
            this.messageTextArea.setText(stringBuilder.toString());
            this.inputMessageTextArea.setText("");
        }
    }


    public JFrame getjFrame() {
        return jFrame;
    }

    public List<JComponent> getList() {
        return list;
    }

    public JPanel getLeftPanel() {
        return leftPanel;
    }

    public JTextArea getMessageTextArea() {
        return messageTextArea;
    }

    public String getUsername() {
        return username;
    }

    public boolean isOpenPrivateWindow() {
        return isOpenPrivateWindow;
    }

    public void setOpenPrivateWindow(boolean openPrivateWindow) {
        isOpenPrivateWindow = openPrivateWindow;
    }

    public List<PrivateChatView> getPrivateChatViewList() {
        return privateChatViewList;
    }

    public void setPrivateChatViewList(List<PrivateChatView> privateChatViewList) {
        this.privateChatViewList = privateChatViewList;
    }
}
